using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NorthwindWeb.Models;

namespace NorthwindWeb.Controllers
{   
    public class ProductController : Controller
    {
		private readonly ISupplierRepository supplierRepository;
		private readonly ICategoryRepository categoryRepository;
		private readonly IProductRepository productRepository;

		// If you are using Dependency Injection, you can delete the following constructor
        public ProductController() : this(new SupplierRepository(), new CategoryRepository(), new ProductRepository())
        {
        }

        public ProductController(ISupplierRepository supplierRepository, ICategoryRepository categoryRepository, IProductRepository productRepository)
        {
			this.supplierRepository = supplierRepository;
			this.categoryRepository = categoryRepository;
			this.productRepository = productRepository;
        }

        //
        // GET: /Product/

        public ViewResult Index()
        {
            ViewBag.PossibleSuppliers = supplierRepository.All;
            ViewBag.PossibleCategories = categoryRepository.All;
            return View(productRepository.AllIncluding(product => product.Supplier, product => product.Category));
        }

        //
        // POST: /Product/Create

        [HttpPost]
        public ActionResult Create([Bind(Exclude = "ProductID")]Product product)
        {
            if (ModelState.IsValid) {
                productRepository.InsertOrUpdate(product);
                productRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleSuppliers = supplierRepository.All;
				ViewBag.PossibleCategories = categoryRepository.All;
                return RedirectToAction("Index");
            }
        }

        //
        // POST: /Product/Edit/5

        [HttpPost]
        public ActionResult Edit(Product product)
        {
            if (ModelState.IsValid) {
                productRepository.InsertOrUpdate(product);
                productRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleSuppliers = supplierRepository.All;
				ViewBag.PossibleCategories = categoryRepository.All;
                return RedirectToAction("Index");
            }
        }

        //
        // POST: /Product/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            productRepository.Delete(id);
            productRepository.Save();

            return RedirectToAction("Index");
        }

        
        public JsonResult productGridData(int rows, int page)
        {
        	var count = productRepository.All.Count();
            var pageData = productRepository.All.OrderBy(x => x.ProductID).Skip((page - 1) * rows).Take(rows).Select(o => new {ProductID = o.ProductID, ProductName = o.ProductName, SupplierID = o.SupplierID, CategoryID = o.CategoryID, QuantityPerUnit = o.QuantityPerUnit, UnitPrice = o.UnitPrice, UnitsInStock = o.UnitsInStock, UnitsOnOrder = o.UnitsOnOrder, ReorderLevel = o.ReorderLevel, Discontinued = o.Discontinued});
            return Json(new { 
            page = page,
            records = count,
            rows = pageData,
            total = Math.Ceiling((decimal)count / rows)
            }, JsonRequestBehavior.AllowGet);
        }
        
        
    }
}

